<?php
/* @var $this CalendarController */

date_default_timezone_set('Europe/Paris');
include("newEventKeeper.php");
include("editEventKeeper.php");
?>


<script>
	$(document).ready(function() {
		 $('#calendar').fullCalendar({
			header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,agendaWeek,agendaDay'
			},
			titleFormat: {
		        month: 'MMMM yyyy',
		        week: "d [ MMMM][ yyyy] {'&#8212;' d MMMM yyyy}",
		        day: 'dddd'
		    },
		    timeFormat: 'H:mm',
			selectable: true,
			selectHelper: true,
			disableDragging: true,
			disableResizing: true,
			allDaySlot: false,
			select: function(start, end, jsEvent, view) {
				resetRepeatingProperties();
				var currentDate = new Date();
				currentDate.setDate(currentDate.getDate()-1);
				currentDate.setHours(23);
				currentDate.setMinutes(59);
				currentDate.setSeconds(59);
				if(start < currentDate)
					return;
				$("#new-event-station").html('');
				var toolsdiv = $("#new-event-avail-tools");
				toolsdiv.html('Brak dostępnych narzędzi');
				
				if((start.getHours()===0&&end.getHours()===0)){				
					setOnDefaultTime(start, end);
				}
				else if(start.getHours() < 6 || end.getHours() > 22)
				{
					alert('Wybrany termin poza dozwolonym czasem (06:00 - 22:00)!');
					setOnDefaultTime(start, end);
				}
				$('#new-event-keeper .reservationdate').val(getISODate(start));
				$('#new-event-keeper .starttime').val(getISOTime(start));
				$('#new-event-keeper .endtime').val(getISOTime(end));
				$( "#new-event-keeper" ).dialog("open");
				$.post(
					'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/availableStationsAndTools',
					{
						day : start.getDate(),
						month : start.getMonth()+1,
						year : start.getFullYear(),
						hourStart:start.getHours(),
						hourStop:end.getHours(),
						minStart:start.getMinutes(),
						minStop:end.getMinutes(),
					},
					function(data) {
						var stations = data.stations;
						var tools = data.tools;
						for ( var i = 0; i < stations.length; i++ ) {
							$("#new-event-station").append(
								'<option value="'+stations[i].id+'">'+stations[i].name+'</option>');
						}
						toolsdiv.html('');
						for ( var i = 0; i< tools.length; i++ ) {
							toolsdiv.append(
								"<input type='checkbox' name='tools[]' id='tool_" + tools[i].id + "' value='" + tools[i].id + "'>"+
								"<label class='checkboxlabel' for='tool_" + tools[i].id + "'>"+tools[i].name+"</label>");
						}
					},
					"json"
				);
		    },
			eventClick: function(calEvent, jsEvent, view) {
				resetProperties();	
		        $("#edit-event-station").html('');
				var toolsdiv = $("#edit-event-avail-tools");				

				$('#edit-event-form .starttime').val(getISOTime(calEvent.start));
				$('#edit-event-form .endtime').val(getISOTime(calEvent.end));
				$('#edit-event-form .reservationdate').val(getISODate(calEvent.start));
				if(calEvent.is_maintained==="1")
					$('#edit-event-form .maintanace').prop('checked',true);					
				$('#edit-event-form .id').val(calEvent.id);
				$("#edit-event-keeper" ).dialog("open");
				$.post(
					'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/availableStationsAndTools',
					{
						day : calEvent.start.getDate(),
						month : calEvent.start.getMonth()+1,
						year : calEvent.start.getFullYear(),
						hourStart:calEvent.start.getHours(),
						hourStop:calEvent.end.getHours(),
						minStart:calEvent.start.getMinutes(),
						minStop:calEvent.end.getMinutes(),
						id:calEvent.id
					},
					function(data) {
						var stations = data.stations;
						var tools = data.tools;
						for ( var i = 0; i < stations.length; i++ ) {
							var temp = '<option value="'+stations[i].id+'">'+stations[i].name+'</option>';
							if(calEvent.stationId==stations[i].id)
								temp = '<option value="'+stations[i].id+'" selected="selected">'+stations[i].name+'</option>';	
							$("#edit-event-station").append(temp);
						}
						toolsdiv.html('');
						for ( var i = 0; i< tools.length; i++ ) {
							var temp2 = "<label class='checkboxlabel' for='tool_" + tools[i].id + "'>"+tools[i].name+"</label>";
							var temp = "<input type='checkbox' name='tools[]' id='tool_" + tools[i].id + "' value='" + tools[i].id + "'>";
							for(var l = 0; l < calEvent.tools.length; l++){
								if(calEvent.tools[l].id == tools[i].id ){
									 temp = "<input type='checkbox' name='tools[]' id='tool_" + tools[i].id + "' value='" + tools[i].id + "' checked>";
								}
							}
							toolsdiv.append(temp+temp2);	
						}
					},
					"json"
				);

		    },
			eventDrop: function(event, delta) {
				//alert(event.title + ' was moved ' + delta + ' days\n' +
				//	'(should probably update your database)');
			},
			eventRender: function(event, element) {
				if(event.is_maintained==="1"){
					element.addClass("maintanace");
				}else{
					element.removeClass("maintanace");
				}
					
				element.children('.fc-event-inner').append(
					'<div>Opis: '+event.description+'</div>'
				);
		    },
			editable: true,
			events: '<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/events'
		});
		
	});

	function getISODateTime(d){
	    // padding function
	    var s = function(a,b){return(1e15+a+"").slice(-b)};

	    // default date parameter
	    if (typeof d === 'undefined'){
	        d = new Date();
	    };

	    // return ISO datetime
	    return s(d.getDate(),2) + '/' +
	    	s(d.getMonth()+1,2) + '/' +
    		d.getFullYear() + ' ' +
	        s(d.getHours(),2) + ':' +
	        s(d.getMinutes(),2);// + ':' +
	        //s(d.getSeconds(),2);
	}

	function getISODate(d){
	    // padding function
	    var s = function(a,b){return(1e15+a+"").slice(-b)};

	    // default date parameter
	    if (typeof d === 'undefined'){
	        d = new Date();
	    };

	    // return ISO datetime
	    return s(d.getDate(),2) + '/' +
	    	s(d.getMonth()+1,2) + '/' +
    		d.getFullYear();
	}

	function getISOTime(d){
	    // padding function
	    var s = function(a,b){return(1e15+a+"").slice(-b)};

	    // default date parameter
	    if (typeof d === 'undefined'){
	        d = new Date();
	    };

	    // return ISO datetime
	    return s(d.getHours(),2) + ':' +
	        s(d.getMinutes(),2);// + ':' +
	        //s(d.getSeconds(),2);
	}

	function setOnDefaultTime(start, end)
	{
		start.setHours(8);
		start.setMinutes(0);
		end.setHours(9);	
		end.setMinutes(0);
	}

</script>
<h1>Harmonogram</h1>
<div id='calendar' class='calendar'></div>
